import { createBrowserRouter, replace } from "react-router";
import Config from "../config";
// 登录
import { Login } from "../views/login";
// 主页
import Main from "../views/main";
import store from "../store";
import { nilV } from "../utils";
const router = createBrowserRouter(
    [
        {
            path: "/",
            loader: () => {
                return replace("/login");
            },
        },
        {
            path: "/login",
            loader: () => {
                // 检查是否已经登录
                let state = store.getState();
                let { user } = state.user;
                if (!nilV(user)) {
                    // 已经登录
                    return replace("/main");
                }
            },
            Component: Login,
        },
        {
            path: "/main",
            Component: Main,
        },
        {
            path: "*",
            loader: () => {
                return replace("/");
            },
        },
    ],
    {
        basename: Config.baseUrl,
    }
);

export { router };
